$(function() {  
    FastClick.attach(document.body);  
    //初始化加载第一条数据
    var id = $('.classifyList .classWord').find('a').attr('data-id');
    initGoodsClass(id);
    
});
var h1=$('.classifyList li').height();  //84
var h2 = $('.heade').height();//77
var h3 = $('footer').height();//84
var h6 = parseInt((document.documentElement.clientHeight-h2-h3)/h1);   //1013-77-84=852
var myScroll;
		function loaded() {
			setTimeout(function(){
				myScroll = new iScroll('wrapper', {
				scrollbarClass: 'myScrollbar', tap: true
			})
			},100)
		
		}
        
		window.addEventListener('load', loaded, false);


//点击
$('.classifyList li').click(function() {
	//获取商品分类ID
	var id = $(this).find('a').attr("data-id");
	$.ajax({
		url : rootPath+"/goodsClass/findIndexByGoodsClass",
		type : "POST",
		data : {"id":id},
		async: false,
		success: function(data,status){
			//清除包含的元素
			$("#classifyShowC").empty();
			var optionHtml = "";
			optionHtml+="<div class='classifyShow_word '><a href='javascript:void(0)'><img src='"+rootPath+"/image/banner1.png' alt='' /></a></div> "
			$.each(data,function(i,result) {
				optionHtml+="<ul class='classifyShow_img oneStyle clearfix'> "
					+" <li>"+result.name+"</li> "
					+" <li> "
					+" <ul class='classifyImg clearfix'> "
					var list = result.children; 
					if(list!=null){
						$.each(list,function(i,goodsClass){
							optionHtml+=" 	<li class='fl'> "
								+" 		<a href='"+rootPath+"/goods/queryGoodsResult?classId="+goodsClass.id+"&flag=1'>"
								var icon = goodsClass.icon;
								if(icon!=null){
									optionHtml+= "<img src='"+iconFile+""+icon+"'/></a>"
								}else{
									optionHtml+= "<img src='"+rootPath+"/image/diandongyueqi.png' /></a> "
								}
								optionHtml+=" <p>"+goodsClass.name+"</p> "
								+" 	</li> "
						});
					}
					optionHtml+=" </ul> "
					+" </li> "
					+" </ul>";
			});
			$("#classifyShowC").append(optionHtml);
		},
		error : function(xhr,status,e){
			alert("服务器出了问题，请检查！"+e);
		}
		
	});
	for(var k = 0; k < $('.classifyList li').length; k++) {
		index = $('.classifyList li').index(this);
	}
	var hh = $('.classifyList').css('top');  
	var hh1= hh.split('px');
	var hh2 = hh1[0];
	var h5 = -($('.classifyList li').length*h1-h6);   //-324  
	var value = -(index*h1)+2*h1;
//	 alert(hh2);
	if($('.classifyList li').length >= 9){
		if(2<=index&&index<$('.classifyList li').length-7){
			//alert(value)
			$('.classifyList').css({top:value+'px'});
		};
	
		if(index>=$('.classifyList li').length-7){
			//alert(2)
			var values =-($('.classifyList li').length-h6)*h1;
			$('.classifyList').css({top:values+'px'});
		}
	};
	$('.classifyList li a').css({
		color: 'rgb(102,102,102)',
		background: 'rgb(220,219,219)'
	
	});
	
	$(this).find('a').css({
		color: 'rgb(202,14,84)',
		background: 'rgb(255,255,255)'
		
	});

});

/***初始化加载数据信息******/
function initGoodsClass(id){
	//获取商品分类ID
	$.ajax({
		url : rootPath+"/goodsClass/findIndexByGoodsClass",
		type : "POST",
		data : {"id":id},
		async: false,
		success: function(data,status){
			//清除包含的元素
			$("#classifyShowC").empty();
			var optionHtml = "";
			optionHtml+="<div class='classifyShow_word '><a href='javascript:void(0)'><img src='"+rootPath+"/image/banner1.png' alt='' /></a></div> "
			$.each(data,function(i,result) {
				optionHtml+="<ul class='classifyShow_img oneStyle clearfix'> "
					+" <li>"+result.name+"</li> "
					+" <li> "
					+" <ul class='classifyImg clearfix'> "
					var list = result.children; 
					if(list!=null){
						$.each(list,function(i,goodsClass){
							optionHtml+=" 	<li class='fl'> "
								+" 		<a href='"+rootPath+"/goods/queryGoodsResult?classId="+goodsClass.id+"&flag=1'>" 
							var icon = goodsClass.icon;
							if(icon!=null){
								optionHtml+= "<img src='"+iconFile+""+icon+"'/></a>"
							}else{
								optionHtml+= "<img src='"+rootPath+"/image/diandongyueqi.png' /></a> "
							}
							optionHtml+=" 		<p>"+goodsClass.name+"</p> "
								+" 	</li> "
						});
					}
					optionHtml+=" </ul> "
					+" </li> "
					+" </ul>";
			});
			$("#classifyShowC").append(optionHtml);
			
		},
		error : function(xhr,status,e){
			alert("服务器出了问题，请检查！"+e);
		}
	});
}
